package cn.iocoder.yudao.module.biz.service.risk.riskMethodName;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskMethodDetail.vo.BizRiskMethodDetailCreateReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskMethodName.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.riskMethodDetail.BizRiskMethodDetailConvert;
import cn.iocoder.yudao.module.biz.convert.risk.riskMethodName.BizRiskMethodNameConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.riskMethodDetail.BizRiskMethodDetailDO;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.riskMethodName.BizRiskMethodNameDO;
import cn.iocoder.yudao.module.biz.dal.mysql.risk.riskMethodDetail.BizRiskMethodDetailMapper;
import cn.iocoder.yudao.module.biz.dal.mysql.risk.riskMethodName.BizRiskMethodNameMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 评级方法主 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizRiskMethodNameServiceImpl implements BizRiskMethodNameService {

    @Resource
    private BizRiskMethodNameMapper riskMethodNameMapper;
    @Resource
    private BizRiskMethodDetailMapper riskMethodDetailMapper;

    @Override
    public String createRiskMethodName(BizRiskMethodNameCreateReqVO createReqVO) {
        // 插入
        BizRiskMethodNameDO riskMethodName = BizRiskMethodNameConvert.INSTANCE.convert(createReqVO);
        riskMethodName.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        riskMethodNameMapper.insert(riskMethodName);
        for(BizRiskMethodDetailCreateReqVO detailVO : createReqVO.getMethodDetail()){
            BizRiskMethodDetailDO bizRiskMethodDetailDO = BizRiskMethodDetailConvert.INSTANCE.convert(detailVO);
            bizRiskMethodDetailDO.setMethodNameId(riskMethodName.getId());
            bizRiskMethodDetailDO.setMethodContent(bizRiskMethodDetailDO.getMethodCount()+"-"+bizRiskMethodDetailDO.getMethodDetail());
            riskMethodDetailMapper.insert(bizRiskMethodDetailDO);
        }
        // 返回
        return riskMethodName.getId();
    }

    @Override
    public void updateRiskMethodName(BizRiskMethodNameUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRiskMethodNameExists(updateReqVO.getId());
        // 更新
        BizRiskMethodNameDO updateObj = BizRiskMethodNameConvert.INSTANCE.convert(updateReqVO);
        riskMethodNameMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskMethodName(String id) {
        // 校验存在
        this.validateRiskMethodNameExists(id);
        // 删除
        riskMethodNameMapper.deleteById(id);
    }

    private void validateRiskMethodNameExists(String id) {
        if (riskMethodNameMapper.selectById(id) == null) {
            throw exception(RISK_METHOD_NAME_NOT_EXISTS);
        }
    }

    @Override
    public BizRiskMethodNameDO getRiskMethodName(String id) {
        return riskMethodNameMapper.selectById(id);
    }

    @Override
    public List<BizRiskMethodNameDO> getRiskMethodNameList(Collection<String> ids) {
        return riskMethodNameMapper.selectBatchIds(ids);
    }

    @Override
    public List<BizRiskMethodNameDO> getAllRiskMethodName() {
        return riskMethodNameMapper.selectList();
    }

    @Override
    public PageResult<BizRiskMethodNameDO> getRiskMethodNamePage(BizRiskMethodNamePageReqVO pageReqVO) {
        return riskMethodNameMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizRiskMethodNameDO> getRiskMethodNameList(BizRiskMethodNameExportReqVO exportReqVO) {
        return riskMethodNameMapper.selectList(exportReqVO);
    }

}
